package jp.ac.waseda.info.ueda.unyo.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import jp.ac.waseda.info.ueda.unyo.manager.ConfManager;

public class ShapePanel extends JPanel {
	
	///////////////////////////////////////////////////////////////////////////
	final static
	public String TITLE = "Shape";
	final static
	public String DEFAULT = "Select...";
	final static
	public String SQUARE = "Square";
	final static
	public String LOAD = "Load Image";
	final static
	public String SELECT = "Select Shape or Image";
	
	static
	private JComboBox loadConf_;
	static
	private JTextField atomName_;	
	static
	private JLabel atomLabel_;
	static
	private JLabel imageLabel_;
	static
	private JButton setImageBt_;
	static
	private JButton removeAllImageBt_;
	static
	private File imageFile_;
	static
	private JPanel p1_;
	static
	private JPanel p2_;
	static
	private JPanel p3_;
	static
	private JComboBox menu_;
	static
	private JScrollPane scroll_;
	static
	private HashMap<String, JPanel> panelMap_ = new HashMap<String, JPanel>();
	
	///////////////////////////////////////////////////////////////////////////
	
	public ShapePanel(){
		addKeyListener(new CommonKeyListener());
		GridBagConstraints gbc = new GridBagConstraints();
		p1_ = new JPanel();
		p1_.setLayout(new BorderLayout());
		p2_ = new JPanel();
		p2_.setLayout(new GridBagLayout());
		p3_ = new JPanel();
		p3_.setLayout(new BoxLayout(p3_, BoxLayout.PAGE_AXIS));
		setLayout(new BorderLayout());
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(2, 2, 2, 2);
		gbc.fill = GridBagConstraints.VERTICAL;
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.anchor = GridBagConstraints.WEST;
		String[] comb = {"Load Shapes"};
		loadConf_ = new JComboBox(comb);
		loadConf_.setFocusable(false);
		
		atomLabel_ = new JLabel("Atom Name :");
		atomName_ = new JTextField();
		atomName_.setColumns(10);
		atomName_.addKeyListener(new CommonKeyListener());
		
		imageLabel_ = new JLabel(SELECT);
		
		setImageBt_ = new JButton("Set");
		setImageBt_.addActionListener(new SetImageListener());
		setImageBt_.setFocusable(false);
		
		removeAllImageBt_ = new JButton("Remove All");
		removeAllImageBt_.addActionListener(new ClearAllImageListener());
		removeAllImageBt_.setFocusable(false);
		
		String[] comb2 = {DEFAULT, SQUARE, LOAD};
		menu_ = new JComboBox(comb2);
		menu_.addActionListener(new ShapeMenuListener());
		menu_.setFocusable(false);
		
		scroll_ = new JScrollPane(p1_);
		
		p2_.add(atomLabel_, gbc);
		gbc.gridx +=1;
		p2_.add(atomName_, gbc);
		gbc.gridx +=1;
		gbc.weightx = 0.1d;
		p2_.add(menu_, gbc);
		gbc.gridx +=1;
		p2_.add(imageLabel_, gbc);
		gbc.gridx +=1;
		gbc.weightx = 0.0;
		p2_.add(setImageBt_, gbc);
		gbc.weightx = 1.0d;
		gbc.gridx +=1;
		p2_.add(removeAllImageBt_, gbc);
		p2_.setBackground(new Color(200, 230, 250));
		p1_.add(p2_, BorderLayout.NORTH);
		p1_.add(p3_, BorderLayout.CENTER);
		add(scroll_,BorderLayout.CENTER);
	}
	
	///////////////////////////////////////////////////////////////////////////
	static
	public void makeImageList(){
		HashMap<String, File> imageFileMap = ConfManager.getImageFiles();
		HashMap<String, String> shapeMap = ConfManager.getShapes();
//		GridBagConstraints gbc = new GridBagConstraints();
//		gbc.gridy = 1;
//		gbc.insets = new Insets(2, 2, 2, 2);
//		gbc.fill = GridBagConstraints.VERTICAL;
//		gbc.weighty = 0.0;
//		gbc.anchor = GridBagConstraints.WEST;
		if(imageFileMap != null){
			Iterator<String> nodeNames = imageFileMap.keySet().iterator();
			while (nodeNames.hasNext()) {
				String nodeName = (String) nodeNames.next();
				File file = imageFileMap.get(nodeName);
				JLabel imageNameLabel = new JLabel("Shape: " + file.getName());
				JLabel nameLabel = new JLabel("Atom Name : " + nodeName);
				JButton removeBt = new JButton("Remove");
				imageNameLabel.setMaximumSize(new Dimension(200,50));
				nameLabel.setMaximumSize(new Dimension(200,50));
				JPanel panel = new JPanel();
				panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
				panel.setAlignmentX(0.01f);
				removeBt.addActionListener(new ClearImageListener(nodeName, panel));
				panel.add(nameLabel);
				panel.add(imageNameLabel);
				panel.add(removeBt);
				p3_.add(panel);
				panelMap_.put(nodeName, panel);
			}
		}
		if(shapeMap != null){
			Iterator<String> nodeNames = shapeMap.keySet().iterator();
			while (nodeNames.hasNext()) {
				String nodeName = (String) nodeNames.next();
				String shape = shapeMap.get(nodeName);
				JLabel imageNameLabel = new JLabel("Shape: " + shape);
				JLabel nameLabel = new JLabel("Atom Name : " + nodeName);
				JButton removeBt = new JButton("Remove");
				imageNameLabel.setMaximumSize(new Dimension(200,50));
				nameLabel.setMaximumSize(new Dimension(200,50));
				JPanel panel = new JPanel();
				panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
				panel.setAlignmentX(0.01f);
				removeBt.addActionListener(new ClearImageListener(nodeName, panel));
				panel.add(nameLabel);
				panel.add(imageNameLabel);
				panel.add(removeBt);
				p3_.add(panel);
				panelMap_.put(nodeName, panel);
			}
		}
	}
	
	static
	private void addImageList(String nodeName, String shape){
		JLabel imageNameLabel = new JLabel("Shape : " + shape);
		JLabel nameLabel = new JLabel("Atom Name : " + nodeName);
		JButton removeBt = new JButton("Remove");
		imageNameLabel.setMaximumSize(new Dimension(200,50));
		nameLabel.setMaximumSize(new Dimension(200,50));
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
		panel.setAlignmentX(0.01f);
		removeBt.addActionListener(new ClearImageListener(nodeName, panel));
		panel.add(nameLabel);
		panel.add(imageNameLabel);
		panel.add(removeBt);
		panelMap_.put(nodeName, panel);
		p3_.add(panel);
		p3_.revalidate();
	}
	
	static
	public class ShapeMenuListener implements ActionListener{

		public void actionPerformed(ActionEvent e) {
			String menu = (String)menu_.getSelectedItem();
			if(menu.equals(LOAD)){
				File imageFile = MainFrame.loadImage();
				if(imageFile == null){
					menu_.setSelectedIndex(0);
					return;
				}
				imageFile_ = imageFile;
				imageLabel_.setText(imageFile_.getPath());
			}else if(menu.equals(DEFAULT)){
				imageLabel_.setText(DEFAULT);
			}else if(menu.equals(SQUARE)){
				imageLabel_.setText(SQUARE);
			}
			menu_.setSelectedIndex(0);
		}
		
	}
	
	static
	public class ClearImageListener implements ActionListener {
		String nodeName_;
		JPanel panel_;
		public ClearImageListener(String nodeName, JPanel panel){
			nodeName_ = nodeName;
			panel_ = panel;
		}
		public void actionPerformed(ActionEvent e){
			HashSet<String> nodes = new HashSet<String>();
			nodes.add(nodeName_);
			ConfManager.removeShapesAndImages(nodes);
			p3_.remove(panel_);
			p3_.repaint();
			p3_.revalidate();
//			makeImageList();
		}
	}
	public class ClearAllImageListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			ConfManager.removeAllShapesAndImages();
			p3_.removeAll();
			p3_.repaint();
			p3_.revalidate();
		}
	}
	public class SetImageListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			String atomName = atomName_.getText();
			String imageLabel = imageLabel_.getText();
			String shape = "";
			if(atomName.isEmpty() && imageLabel.equals(SELECT)){
				MainFrame.errorWindow("Please write an atom name and select a shape or load an image file.",
						false);
				return;
			}
			if(atomName.isEmpty()){
				MainFrame.errorWindow("Please write an atom name.", false);
				return;
			}
			if(imageLabel.equals(SELECT)){
				MainFrame.errorWindow("Please select a shape or load an image file.", false);
				return;
			}
			if(imageFile_ == null && !imageLabel.equals(SQUARE)) return;
			if(imageFile_ !=null) shape = imageFile_.getName();
			if(panelMap_.containsKey(atomName)){
				JPanel panel = panelMap_.get(atomName);
				p3_.remove(panel);
				p3_.repaint();
				p3_.revalidate();
				panelMap_.remove(atomName);
				if(imageLabel.equals(SQUARE)){
					HashSet<String> removeNodes = new HashSet<String>();
					removeNodes.add(atomName);
					ConfManager.removeShapesAndImages(removeNodes);
				}
			}
			if(imageLabel.equals(SQUARE)){
				HashMap<String, String> map = new HashMap<String, String>();
				map.put(atomName, imageLabel);
				ConfManager.setShapes(map);
				shape = imageLabel_.getText();
			}
			if(imageFile_ != null){
				HashMap<String, File> map = new HashMap<String, File>();
				map.put(atomName, imageFile_);
				ConfManager.setImages(map);
			}else{
				HashMap<String, String> map = new HashMap<String, String>();
				map.put(atomName, shape);
				ConfManager.setShapes(map);
			}
			addImageList(atomName, shape);
		}
	}
	public class LoadImageListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			File imageFile = MainFrame.loadImage();
			if(imageFile == null) return;
			imageFile_ = imageFile;
			imageLabel_.setText(imageFile_.getPath());
		}
	}
}